// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <pollux/common/base/pointers.h>

#include <gtest/gtest.h>
#include <pollux/testing/gtest_utils.h>

using namespace kumo::pollux;
namespace {
TEST(PointersTest, uniquePtrConvert) {
  class Foo1 {
   public:
    explicit Foo1(int32_t a) : a_(a) {}
    virtual ~Foo1() = default;

    int32_t a() const {
      return a_;
    }

   protected:
    int32_t a_;
  };

  class Foo2 : public Foo1 {
   public:
    Foo2(int32_t a, int32_t b) : Foo1(a), b_(b) {}

    int32_t b() const {
      return b_;
    }

   private:
    int32_t b_;
  };

  Foo2* rawFoo2 = new Foo2(10, 20);
  ASSERT_EQ(rawFoo2->a(), 10);
  ASSERT_EQ(rawFoo2->b(), 20);
  std::unique_ptr<Foo1> foo1(rawFoo2);
  std::unique_ptr<Foo2> foo2;
  castUniquePointer(std::move(foo1), foo2);
  ASSERT_TRUE(foo2 != nullptr);
  ASSERT_EQ(foo2->a(), 10);
  ASSERT_EQ(foo2->b(), 20);

  class Foo3 {
   public:
    explicit Foo3(int32_t c) : c_(c) {}

   private:
    [[maybe_unused]] int32_t c_; // Retained for testing
  };
  std::unique_ptr<Foo3> foo3;

  ASSERT_ANY_THROW(castUniquePointer(std::move(foo2), foo3));
  ASSERT_TRUE(foo3 == nullptr);
}
} // namespace
